package models

import (
	"gorm.io/gorm/schema"
	"strings"
)

// MetricData 测点历史数据表
type MetricData struct {
	Id int64 `json:"id" gorm:"primaryKey"`
	SqlCreateTimeFields
	SqlUpdateTimeFields

	MetricId  int64  `json:"metric_id" gorm:"column:metric_id;index:idx_property,priority:1,unique"`
	Value     string `json:"value" gorm:"type:varchar(64);" `
	Timestamp int64  `json:"timestamp" gorm:"index:idx_property,priority:2,unique"`
}

func (m *MetricData) TableName() string {
	return "metric_data"
}

func (m *MetricData) SplitTableName(key string) string {
	//key不要带点，会有问题
	key = strings.Replace(key, ".", "_", -1)

	return m.TableName() + "_" + key
}

// MetricLatestData 最新数据表
type MetricLatestData struct {
	Id int64 `json:"id" gorm:"primaryKey"`
	SqlCreateTimeFields
	SqlUpdateTimeFields

	MetricId  int64  `json:"metric_id" gorm:"column:metric_id;index:idx_property,priority:1,unique"`
	Value     string `json:"value" gorm:"type:varchar(64);" `
	Timestamp int64  `json:"timestamp" gorm:"column:timestamp"`
}

func (m *MetricLatestData) TableName(namer schema.Namer) string {
	return namer.TableName("metric_latest_data")
}
